package PTA_JAVA.JavaB.Javademo20;

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] types = {"Student","Teacher"};
        //输入种子seed，并用其初始化Random对象
        int seed = Integer.parseInt(sc.next());
        Random random = new Random(seed);
        //输入对象生成数量n
        int n = Integer.parseInt(sc.next());
        Person[] person = new Person[n];
        //以下是生成n个对象的循环
        for (int i = 0; i < n; i++) {
            //使用random.nextInt(2)从types数组中随机选取元素x并输出
            String x=types[random.nextInt(2)];
            //根据x选择输入相应的参数
            if(x.equals("Student")){
                Student stu = new Student(i,sc.next(), sc.next(),Integer.parseInt(sc.next()),sc.next());
                System.out.println("Student");
                person[i]=stu;
            }else if(x.equals("Teacher")){
                Teacher tea = new Teacher(i,sc.next(), sc.next(),sc.next(),Double.parseDouble(sc.next()));
                System.out.println("Teacher");
                person[i]=tea;
            }
        }
        //输出person集合进行降序排序并输出提示信息
        System.out.println("afterSortDES:");
        Arrays.sort(person,new PerComparatorDES());
        //输出排序后集合的toString
        for (Person i:person){
            System.out.println(i.toString());
        }
        //输出person集合进行升序排序并输出提示信息
        System.out.println("afterSortAES:");
        Arrays.sort(person,new PerComparatorAES());
        //输出排序后集合的toString
        for (Person i:person){
            System.out.println(i.toString());
        }
        //输出teacher集合进行salary降序排序并输出提示信息
        System.out.println("afterSortTeacher:");
        Teacher[] teacher= Arrays.stream(person).filter(i ->i instanceof Teacher).toArray(Teacher[]::new);
        Arrays.sort(teacher,new TeaComparator());
        //输出排序后集合的toString
        for (Teacher i:teacher){
            System.out.println(i.toString());
        }
        //请在此粘贴上检验程序代码
        Class interfaces1[] = PerComparatorDES.class.getInterfaces();
        Class interfaces2[] = PerComparatorAES.class.getInterfaces();
        Class interfaces3[] = TeaComparator.class.getInterfaces();
        for(Class inf:interfaces1) {
            System.out.println(inf);
        }
        for(Class inf:interfaces2) {
            System.out.println(inf);
        }
        for(Class inf:interfaces3) {
            System.out.println(inf);
        }
    }
}
class Person{
    private int id;
    private String name;
    private String birthDate;

    public Person(int id, String name, String birthDate) {
        this.id = id;
        this.name = name;
        this.birthDate = birthDate;
    }

    public int getId() {
        return id;
    }

    public String getBirthDate() {
        return birthDate;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person [" +
                "id=" + id +
                ", name=" + name +
                ", birthDate=" + birthDate +
                "]";
    }
}
class Student extends Person{
    private int enrollmentYear;
    private String clazzName;

    public Student(int id, String name, String birthDate, int enrollmentYear, String clazzName) {
        super(id, name, birthDate);
        this.enrollmentYear = enrollmentYear;
        this.clazzName = clazzName;
    }

    @Override
    public String toString() {
        return super.toString()+"\n"+"Student [" +
                "enrollmentYear=" + enrollmentYear +
                ", clazzName=" + clazzName +
                "]";
    }
}
class Teacher extends Person{
    private String department;
    private double salary;

    public Teacher(int id, String name, String birthDate, String department, double salary) {
        super(id, name, birthDate);
        this.department = department;
        this.salary = salary;
    }

    @Override
    public String toString() {
        return super.toString()+"\n"+"Teacher [" +
                "department=" + department +
                ", salary=" + salary +
                "]";
    }

    public double getSalary() {
        return salary;
    }
}
class PerComparatorDES implements Comparator<Person> {
    @Override
    public int compare(Person p1, Person p2) {
        if(p1.getName().compareTo(p2.getName())>0){
            return -1;
        }else if(p1.getName().compareTo(p2.getName())<0){
            return 1;
        }else{
            if(p1.getId()-p2.getId()>0){
                return -1;
            }else if(p1.getId()-p2.getId()<0){
                return 1;
            }
        }
        return 0;
    }
}
class PerComparatorAES implements Comparator<Person>{
    @Override
    public int compare(Person p1, Person p2) {
        if(p1.getName().compareTo(p2.getName())>0){
            return 1;
        }else if(p1.getName().compareTo(p2.getName())<0){
            return -1;
        }else{
            if(p1.getId()-p2.getId()>0){
                return 1;
            }else if(p1.getId()-p2.getId()<0){
                return -1;
            }
        }
        return 0;
    }
}
class TeaComparator implements Comparator<Teacher>{
    @Override
    public int compare(Teacher t1, Teacher t2) {
        if(t1.getSalary()-t2.getSalary()>0){
            return -1;
        }else if(t1.getSalary()-t2.getSalary()<0){
            return 1;
        }
        return 0;
    }
}


